package cn.jobstart.pubInter;

import cn.jobstart.pubInter.exception.DaoException;
import cn.jobstart.pubInter.exception.DaoExceptionEnum;
import cn.jobstart.pubInter.exception.SkException;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* @author sunke
* @Date 2021-02-26 14:00:10
* @description
*/

@Slf4j
public abstract class AbstractBaseDao<T extends BaseIdDoMain<KeyType>, TS extends BaseSearch, KeyType> extends AbstractBaseFindDao<T, TS, KeyType> {
    @Override
    public abstract IBaseMapper<T, TS, KeyType> getMapper();

    public int insert(T t) throws SkException {
        if (t == null) {
            return 0;
        }
        setIdAndTime(t);
        try {
            return getMapper().insert(t);
        } catch (Exception e) {
             log.error(e.getMessage(),e);
            throw new DaoException(DaoExceptionEnum.DAO_INSERT_ERROR);
        }
    }

    public int delete(KeyType id) throws SkException {
        try {
            return getMapper().delete(id);
        } catch (Exception e) {
             log.error(e.getMessage(),e);
            throw new DaoException(DaoExceptionEnum.DAO_DEL_ERROR);
        }
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     * @throws SkException
     */
    public int logicDelete(KeyType id) throws SkException {
        try {
            return getMapper().logicDelete(id);
        } catch (Exception e) {
             log.error(e.getMessage(),e);
            throw new DaoException(DaoExceptionEnum.DAO_DEL_ERROR);
        }
    }

    public int batchDelete(@Param("list") List<KeyType> list) throws SkException {
        if (list == null || list.size() == 0) {
            return 0;
        }
        try {
            return getMapper().batchDelete(list);
        } catch (Exception e) {
             log.error(e.getMessage(),e);
            throw new DaoException(DaoExceptionEnum.DAO_DEL_ERROR);
        }
    }

    public int update(T t) throws SkException {
        if (t == null) {
            return 0;
        }
        if (t instanceof BaseTimeDoMain) {
            ((BaseTimeDoMain) t).setUpdateTime(new Date());
        }
        try {
            return getMapper().update(t);
        } catch (Exception e) {
             log.error(e.getMessage(),e);
            throw new DaoException(DaoExceptionEnum.DAO_UPDATE_ERROR);
        }
    }

    public int updateByUpdateTime(T t) throws SkException {
        if (t == null) {
            return 0;
        }
        if (t instanceof BaseTimeDoMain) {
            ((BaseTimeDoMain) t).setUpdateTime(new Date());
        }
        try {
            return getMapper().updateByUpdateTime(t);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            throw new DaoException(DaoExceptionEnum.DAO_UPDATE_ERROR);
        }
    }

    public int updateByVersion(T t) throws SkException {
        if (t == null) {
            return 0;
        }
        if (t instanceof BaseTimeDoMain) {
            ((BaseTimeDoMain) t).setUpdateTime(new Date());
        }
        try {
            return getMapper().updateByVersion(t);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            throw new DaoException(DaoExceptionEnum.DAO_UPDATE_ERROR);
        }
    }



    /**
     * 批量添加
     *
     * @param list
     * @return
     */
    public int batchInsert(List<T> list) throws SkException {
        if (list == null || list.size() == 0) {
            return 0;
        }
        for (T t : list) {
            setIdAndTime(t);
        }
        try {
            return getMapper().batchInsert(list);
        } catch (Exception e) {
             log.error(e.getMessage(),e);
            throw new DaoException(DaoExceptionEnum.DAO_INSERT_ERROR);
        }
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 设置主键和创建时间和更新时间
     *
     * @param t
     */
    private void setIdAndTime(T t) {
        if (t instanceof BaseIdDoMain) {
            if (((BaseIdDoMain) t).getId() == null) {
                try {
                    ((BaseTimeDoMain) t).setId(AbstractBaseDao.uuid());  
                }catch(Exception e){
                }
            }
        }
        if (t instanceof BaseTimeDoMain) {
            Date current = new Date();
            if (((BaseTimeDoMain) t).getCreateTime() == null) {
                ((BaseTimeDoMain) t).setCreateTime(current);
            }
            if (((BaseTimeDoMain) t).getUpdateTime() == null) {
                ((BaseTimeDoMain) t).setUpdateTime(current);
            }
        }
    }
    /**
    * 按map里的数据更新
    * @param dataMap ，key 是字段名 value 要更新的数据
    * @return
    */
    public boolean updateDataBySearch(Map<String,Object>dataMap,TS search) throws SkException{
       try{
            return getMapper().updateDataBySearch(dataMap,search)>0;
        }catch(Exception e){
             log.error(e.getMessage(),e);
            return false;
        }
    }

    /**
    * 按map里的数据更新
    * @param dataMap ，key 是字段名 value 要更新的数据
    * @return
    */
    public boolean updateDataById(Map<String,Object>dataMap,KeyType id) throws SkException{
        try{
            return getMapper().updateDataById(dataMap,id)>0;
        }catch(Exception e){
             log.error(e.getMessage(),e);
            return false;
        }

    }


}
